package com.itcast.donkey.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.adapters.DetailedTopicAdapter;
import com.itcast.donkey.modles.DetailedTopicModles;
import com.itcast.donkey.modles.UriModles;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 详细的专题内容
 * AttackFragment-->ScenicSpotActivity-->TopicActivity-->DetailedTopicActivity
 * Created by lanouhn on 16/8/27.
 */
public class DetailedTopicActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private ImageView ivStarJourney;
    private ImageView ivShareJourney;
    private RecyclerView recyclerViewDetailedTopic;
    private boolean isCollect = true;
    private DetailedTopicAdapter adapter;
    private DetailedTopicModles modles;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_topic);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        ivStarJourney = (ImageView) findViewById(R.id.iv_star_journey);
        ivShareJourney = (ImageView) findViewById(R.id.iv_share_journey);
        recyclerViewDetailedTopic = (RecyclerView) findViewById(R.id.recyclerView_detailed_topic);


    }

    private void initData() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        final String uri = UriModles.ATTACK_CLICK_FRONT_DETAILED_TOPIC_URL + id + UriModles.ATTACK_CLICK_END_DETAILED_TOPIC_URL;

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(uri)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String values = response.body().string();
                        Gson gson = new Gson();
                        modles = gson.fromJson(values, DetailedTopicModles.class);
                        if (modles.getArticle_sections().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }

                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(DetailedTopicActivity.this, "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    LinearLayoutManager lineManager = new LinearLayoutManager(DetailedTopicActivity.this);
                    recyclerViewDetailedTopic.setLayoutManager(lineManager);


                    adapter = new DetailedTopicAdapter(DetailedTopicActivity.this, modles);
                    recyclerViewDetailedTopic.setAdapter(adapter);
                    break;
                case 2:
                    Toast.makeText(DetailedTopicActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @TargetApi(Build.VERSION_CODES.M)
    private void initEvent() {

        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
        ivStarJourney.setOnClickListener(this);
        ivShareJourney.setOnClickListener(this);


        //悬停效果，还不会写
        recyclerViewDetailedTopic.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
            case R.id.iv_star_journey:
                if (isCollect) {
                    ivStarJourney.setBackgroundResource(R.mipmap.star_24_white_full);
                    isCollect = false;
                    Toast.makeText(this, "已收藏", Toast.LENGTH_SHORT).show();
                } else {
                    ivStarJourney.setBackgroundResource(R.mipmap.star_24_white_empty);
                    Toast.makeText(this, "已取消", Toast.LENGTH_SHORT).show();
                    isCollect = true;
                }
                break;
            case R.id.iv_share_journey:
                /**
                 * 分享的弹出框
                 */
                AlertDialog.Builder builder = new AlertDialog.Builder(DetailedTopicActivity.this);
                View view = View.inflate(DetailedTopicActivity.this, R.layout.dialog_scenic_spot, null);
                builder.setView(view);
                builder.setCancelable(false);
                final AlertDialog ad = builder.create();
                TextView btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.cancel();
                    }
                });
                ad.show();
                break;

        }
    }
}
